# -*- coding:utf-8 -*-
"""
word 2 emotion
the word strong indicate a specific emotion
"""
import simplejson

w2emo = {
        #u'为什么':-1,
        #u"为甚么":-1,
        #u"泪":-1,
        }

def predict_2_word(lrc_str,w_list=None):
    """
    : params lrc_str : the string for lyric file
    : params w_list : word list to be check, if w_list is None, check all the w in w2emo
    : return label and confidence
    """
    if w_list == None:
        w_list = w2emo.keys()
    pos_count = 0
    neg_count = 0
    for w in w_list:
        if not w2emo.has_key(w):
            continue

        if w2emo[w] == 1:
            pos_count += lrc_str.count(w)
        elif w2emo[w] == -1:
            neg_count += lrc_str.count(w)
        else:
            raise Exception('why here')
        
    if pos_count + neg_count == 0:
        return (0,0)
    elif pos_count > neg_count:
        return (1,pos_count / float(pos_count+neg_count) )
    elif neg_count > pos_count:
        return (-1,neg_count / float(pos_count+neg_count) )

###
# the following part is the experiment part
###
def batch_run():
    from gdsettings import GD_DIR
    from commonprocess.read_ano import read_mid_p
    from pyweka.classifierresult import get_result_from_two_list,get_cm_from_two_list

    tr_mid_list = simplejson.load(open('%s/data/vote_split/train%d.json'%(GD_DIR,0)))
    te_mid_list = simplejson.load(open('%s/data/vote_split/test%d.json'%(GD_DIR,0)))
    mid_list = tr_mid_list+te_mid_list
    mid2p = read_mid_p()
    
    r_list = []
    p_list = []
    for mid in mid_list:
        # try to predict
        lrc_str = open('/Users/wangxing/gd/lyrics/%s.lrc'%(mid)).read()
        l,c = predict_2_word(lrc_str.decode('gbk'))
        if c > 0:
            p_list.append( 1 if l > 0 else 0)
            r_list.append(1 if mid2p[mid] > 5 else 0)
    rdic = get_result_from_two_list(r_list,p_list)
    cm = get_cm_from_two_list(r_list,p_list)
    print rdic
    print cm

if __name__ == "__main__":
    """
    result show that it is not a simple task
    """
    batch_run()
